﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;
using SharpDiary.Core.Attributes;
using SharpDiary.Core.Common;

namespace SharpDiary.Diary
{
    /// <summary>
    /// 添加新格言。
    /// </summary>
    [GuiModule("28536e03-e892-417b-983c-3127d1b2c1e0", "AppendMemo", "Ray Shaw(demogodyou@gmail.com)")]
    public partial class frmAppendMemo : GuiModuleUIBase
    {
        public frmAppendMemo()
        {
            InitializeComponent();
        }

        private void txtContent_TextChanged(object sender, EventArgs e)
        {
            btnOk.Enabled = txtContent.Text.Trim().Length > 0;
        }

        /// <summary>
        /// 检查输入数据合法性。
        /// </summary>
        /// <returns></returns>
        private bool CheckIfInputOk()
        {
            if (txtContent.Text.Trim().Length == 0)
            {
                ShowMessage("您还没有输入格言内容。请输入内容后再点击确定。");
                txtContent.Focus();

                return false;
            }

            if (MemoInfo.Exists(txtContent.Text))
            {
                ShowMessage("您输入的格言内容已经存在。请输入其它格言内容。");
                txtContent.Focus();
                txtContent.SelectAll();

                return false;
            }

            return true;
        }

        /// <summary>
        /// 保存。
        /// </summary>
        /// <returns></returns>
        private bool Save()
        {
            MemoInfo memoInfo = new MemoInfo();
            memoInfo.Content = txtContent.Text;

            return MemoInfo.Create<MemoInfo>(memoInfo);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!CheckIfInputOk()) { return; }

            btnOk.Enabled = false;
            Application.DoEvents();

            try
            {
                if (Save())
                {
                    txtContent.Clear();

                    ShowMessage("保存成功。您可以继续添加新的格言或返回。");
                }
                else
                {
                    ShowMessage("保存失败。");
                }
            }
            finally
            {
                btnOk.Enabled = txtContent.Text.Trim().Length > 0;
                Application.DoEvents();
            }
        }

        #region IGuiModule 成员

        public void Display()
        {
            this.ShowDialog();
        }

        #endregion

        #region INeotomaModule 成员

        private IShell _shell;

        public IShell OwnerShell
        {
            get
            {
                return _shell;
            }
            set
            {
                _shell = value;
            }
        }

        public event EventHandler<ModuleDisposedEventArgs> ModuleDisposed;

        #endregion
    }
}
